package com.mystyle.le.weekproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.beans.Seniority;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Random;

/**
 * Created by le on 2016/10/12.
 */
public class SoftImageAdapter extends BaseAdapter {
    private List<Seniority> seniorityList;
    private Context context;

    public SoftImageAdapter(List<Seniority> seniorityList, Context context) {
        this.seniorityList = seniorityList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return seniorityList==null?0:seniorityList.size();
    }

    @Override
    public Object getItem(int position) {
        return seniorityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.soft_img_layout,parent,false);
            holder = new ViewHolder();
            holder.picture_IV = (ImageView) convertView.findViewById(R.id.picture_IV1);
            holder.softImg_tv = (TextView) convertView.findViewById(R.id.softImg_tv);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Random random = new Random();
        int[] arr = new int[3];
        if (position!=-1){
            for (int i =0;i<3;i++){
                arr[i] = random.nextInt(seniorityList.size());
                Seniority seniority = seniorityList.get(arr[i]);
                String name = seniority.getName();
                holder.softImg_tv.setText(name);
                String icon = seniority.getIcon();
                Picasso.with(context).load(icon).into(holder.picture_IV);
            }
//            this.notifyDataSetChanged();//刷新数据
        }

        return convertView;
    }
    class ViewHolder{
        ImageView picture_IV;
        TextView softImg_tv;
    }
}
